package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"user-srv/handler/model"
)

var DB *gorm.DB
var err error

func MysqlInit() {
	r := Config.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/day20?charset=utf8mb4&parseTime=True&loc=Local",
		r.User,
		r.Password,
		r.Host,
		r.Port,
	)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("数据库连接失败")
		return
	} else {
		fmt.Println("数据库连接成功")
	}
	err = DB.AutoMigrate(&model.User{})
	if err != nil {
		panic("迁移失败")
		return
	} else {
		fmt.Println("迁移成功")
	}
	
}
